<?php
namespace App\Http\Controllers\Api\v1\Astro;
use App\Http\Controllers\Api\BaseController;
use App\Http\Controllers\Api\v1\Astro\DataController;
use App\Http\Controllers\Api\v1\User\AstrosetController;
use App\Models\Api\User;
use Illuminate\Http\Request;


class NowController extends BaseController
{
    public function get(Request $request)
    {
        $datetime = htmlspecialchars($request->input('datetime'));
        $ucid = htmlspecialchars($request->input('ucid'));
        $longitude = number_format(floatval($request->input('longitude')),8);
        $latitude = number_format(floatval($request->input('latitude')),8);
        $time = strtotime($datetime);
        $timeinfo = [
            'year'=>date('Y',$time),
            'month'=>date('m',$time),
            'day'=>date('d',$time),
            'hour'=>date('H',$time),
            'minute'=>date('i',$time),
        ];
        $timeinfo['timezone'] = intval($request->input('timezone'));
        $timeinfo['is_summer'] = isSummerTime($timeinfo['hour'],$timeinfo['minute'],0,
            $timeinfo['month'],$timeinfo['day'],$timeinfo['year']);

        $Data = new DataController();

        $timestamp1 = $Data->deal_time($timeinfo);

        $utdatenow = strftime("%d.%m.%Y", $timestamp1);
        $utnow = strftime("%H:%M:%S", $timestamp1);

        $Set = new AstrosetController();
        $astro_set = $Set->get(true,'now');// 内部调用
        $h_sys = $astro_set['value']['h_sys'];

        $Data->planets_display = $astro_set['value']['planets_display'];
        $Data->asps_display = $astro_set['value']['asps_display'];
        $Data->asp_orb_all = $astro_set['value']['asp_orb_all'];
        $Data->planet_orb_all = $astro_set['value']['planet_orb_all'];

        $options = [
            'utdatenow' => $utdatenow,
            'utnow' => $utnow,
            'longitude' => $longitude,
            'latitude' => $latitude,
            'h_sys' => $astro_set['value']['h_sys'],
        ];

        $out = $Data->execData($options);

        $original_data = $Data->getOriginalData($out,$h_sys);
        $Data->array_remove($original_data,$Data->planets_display);
        $details = $Data->getPlanetHouseDetail($original_data, $original_data['hc1'],$timeinfo['year']);
        $phase = $Data->planetPkPlanet($original_data, $original_data, true);
        foreach ($phase['planets'] as $k => $d)
        {
            $details['planets'][$k]['phase'] = $d['phase'];
            if(empty($d['phase'])) $details['planets'][$k]['phase'] = null;
        }
        $details['phase'] = $phase['phase'];
        $details['characteristic'] = $Data->recept_or_mutual_recept($details['planets']);

        $details['fortune'] = $this->get_fort_score($details,$Data->asp_orb_all,$timeinfo['year']);// 福点分数

        $details = $this->get_structure($details);

        $details = $this->get_house_info($utdatenow,$utnow,$longitude,$latitude,$details);

        $details['planets'] = (object)$details['planets'];// 指定输出为object  防止所有行星全开时 类型改变
        $details['characteristic'] = (object)$details['characteristic'];// 指定输出为object  防止类型改变

        return response()->json(['code'=>200,'data'=>$details,'msg'=>'Success.']);
    }

    /**
     * 计算古典七星角宫  果宫   续宫
     */
    private function get_house_info($utdatenow,$utnow,$longitude,$latitude,$details)
    {
        $ret = $details;
        $exec_str1 = config('astrolog.prefix')." -b$utdatenow -ut$utnow -p0123456789 -eswe -house$longitude,$latitude,p -fldj -g, -head";
        $exec_str2 = config('astrolog.prefix')." -b$utdatenow -ut$utnow -p0123456789 -eswe -house$longitude,$latitude,w -fldj -g, -head";
        exec ($exec_str1, $out1);
        exec ($exec_str2, $out2);

        for($i = 0;$i < 7; $i++)
        {
            $d1 = @explode(',',$out1[$i])[2] * 1;
            $d2 = @explode(',',$out2[$i])[2] * 1;

            $h1 = floor($d1);
            $h2 = floor($d2);

            if($h1 % 3 == 1) $ret['influence']['p']['angular'][] = $i;
            elseif($h1 % 3 == 2) $ret['influence']['p']['continues'][] = $i;
            elseif($h1 % 3 == 0) $ret['influence']['p']['apoklimas'][] = $i;

            if($h2 % 3 == 1) $ret['influence']['w']['angular'][] = $i;
            elseif($h2 % 3 == 2) $ret['influence']['w']['continues'][] = $i;
            elseif($h2 % 3 == 0) $ret['influence']['w']['apoklimas'][] = $i;
        }
        $ret['according'] = ['just'=>[],'reverse'=>[]];
        $ds = [];
        for($i=0;$i<10;$i++)
        {
            $d = explode(',',$out1[$i])[1] * 1;
            $ds[] = $d;
        }
        foreach ($ds as $k1 => $d1)
        {
            foreach ($ds as $k2 => $d2)
            {
                if($k1 < $k2 && abs(abs($d1) -abs($d2)) <= 1)
                {
                    // 反纬照
                    if($d1 * $d2 < 0)
                    {
                        $ret['according']['reverse'][] = [$k1,$k2];
                    }
                    // 纬照
                    else
                    {
                        $ret['according']['just'][] = [$k1,$k2];
                    }
                }
            }
        }
        return $ret;
    }
    /**
     * 计算福点得分
     * @param array $details
     * @param array $asp_orb_all
     * @param int $year
     * @return array
     */
    private function get_fort_score($details = [],$asp_orb_all = [],$year = 1993)
    {
        $sign_score = 0;
        $house_score = 0;
        $phase_score = 0;
        $star_score = 0;


        $asc = $details['planets'][ASC]['deg'];
        $sun = $details['planets'][0]['deg'];
        $moon = $details['planets'][1]['deg'];
        $day = ($details['planets'][0]['in_house_id'] > 6) ? true : false;// 是否为日间盘
        $fort = ($day) ? ($asc + $moon - $sun) : ($asc - $moon + $sun);
        if($fort >= 360) $fort -= 360;
        elseif($fort < 0) $fort += 360;

        $in_sign_id = InWhichSign($fort);
        $in_sign_deg = $fort - ( $in_sign_id - 1 ) * 30;

        ###################
        // 福点位于金牛座或双鱼座(+5)
        if(in_array($in_sign_id,[2,12]))
        {
            $sign_score += 5;
        }
        // 福点在天秤、射手、狮子或巨蟹(+4)
        elseif(in_array($in_sign_id,[7,9,5,4]))
        {
            $sign_score += 4;
        }
        // 福点在双子(+3)
        elseif($in_sign_id == 3)
        {
            $sign_score += 3;
        }
        // 福点在天蝎、摩羯或水瓶(-5)
        elseif(in_array($in_sign_id,[8,10,11]))
        {
            $sign_score += 4;
        }
        // 福点在白羊(0)
        elseif($in_sign_id == 1)
        {
            $sign_score += 0;
        }
        // 福点在处女时
        elseif($in_sign_id == 6)
        {
            // 合金星和木星守护的界(+2)
            if($in_sign_deg >= 17 && $in_sign_deg < 28)
            {
                $sign_score += 2;
            }
        }
        // 福点合金星和木星界(+6)
        if(
            // 白羊座
            (
                $in_sign_id == 1
                &&
                (
                    ($in_sign_deg >= 0 && $in_sign_deg < 6)
                    || ($in_sign_deg >= 6 && $in_sign_deg < 12)
                )
            )
            ||
            // 金牛座
            (
                $in_sign_id == 2
                &&
                (
                    ($in_sign_deg >= 0 && $in_sign_deg < 8)
                    || ($in_sign_deg >= 14 && $in_sign_deg < 22)
                )
            )
            ||
            // 双子座
            (
                $in_sign_id == 3
                &&
                (
                    ($in_sign_deg >= 12 && $in_sign_deg < 17)
                    || ($in_sign_deg >= 6 && $in_sign_deg < 12)
                )
            )
            ||
            // 巨蟹座
            (
                $in_sign_id == 4
                &&
                (
                    ($in_sign_deg >= 7 && $in_sign_deg < 13)
                    || ($in_sign_deg >= 19 && $in_sign_deg < 26)
                )
            )
            ||
            // 狮子座
            (
                $in_sign_id == 5
                &&
                (
                    ($in_sign_deg >= 0 && $in_sign_deg < 6)
                    || ($in_sign_deg >= 6 && $in_sign_deg < 11)
                )
            )
            ||
            // 天秤座
            (
                $in_sign_id == 7
                &&
                (
                    ($in_sign_deg >= 21 && $in_sign_deg < 28)
                    || ($in_sign_deg >= 14 && $in_sign_deg < 21)
                )
            )
            ||
            // 天蝎座
            (
                $in_sign_id == 8
                &&
                (
                    ($in_sign_deg >= 7 && $in_sign_deg < 11)
                    || ($in_sign_deg >= 19 && $in_sign_deg < 24)
                )
            )
            ||
            // 射手座
            (
                $in_sign_id == 9
                &&
                (
                    ($in_sign_deg >= 12 && $in_sign_deg < 17)
                    || ($in_sign_deg >= 0 && $in_sign_deg < 12)
                )
            )
            ||
            // 摩羯座
            (
                $in_sign_id == 10
                &&
                (
                    ($in_sign_deg >= 14 && $in_sign_deg < 22)
                    || ($in_sign_deg >= 7 && $in_sign_deg < 14)
                )
            )
            ||
            // 水瓶座
            (
                $in_sign_id == 11
                &&
                (
                    ($in_sign_deg >= 7 && $in_sign_deg < 13)
                    || ($in_sign_deg >= 13 && $in_sign_deg < 20)
                )
            )
            ||
            // 双鱼座
            (
                $in_sign_id == 12
                &&
                (
                    ($in_sign_deg >= 0 && $in_sign_deg < 12)
                    || ($in_sign_deg >= 12 && $in_sign_deg < 16)
                )
            )
        )
        {
            $sign_score += 6;
        }
        // 福点部分合土星或火星守护的界(-5)
        if(
            // 白羊座
            (
                $in_sign_id == 1
                &&
                (
                    ($in_sign_deg >= 20 && $in_sign_deg < 25)
                    || ($in_sign_deg >= 25 && $in_sign_deg < 30)
                )
            )
            ||
            // 金牛座
            (
                $in_sign_id == 2
                &&
                (
                    ($in_sign_deg >= 27 && $in_sign_deg < 30)
                    || ($in_sign_deg >= 22 && $in_sign_deg < 27)
                )
            )
            ||
            // 双子座
            (
                $in_sign_id == 3
                &&
                (
                    ($in_sign_deg >= 17 && $in_sign_deg < 24)
                    || ($in_sign_deg >= 24 && $in_sign_deg < 30)
                )
            )
            ||
            // 巨蟹座
            (
                $in_sign_id == 4
                &&
                (
                    ($in_sign_deg >= 0 && $in_sign_deg < 7)
                    || ($in_sign_deg >= 26 && $in_sign_deg < 30)
                )
            )
            ||
            // 狮子座
            (
                $in_sign_id == 5
                &&
                (
                    ($in_sign_deg >= 24 && $in_sign_deg < 30)
                    || ($in_sign_deg >= 11 && $in_sign_deg < 18)
                )
            )
            ||
            // 处女座
            (
                $in_sign_id == 6
                &&
                (
                    ($in_sign_deg >= 21 && $in_sign_deg < 28)
                    || ($in_sign_deg >= 28 && $in_sign_deg < 30)
                )
            )
            ||
            // 天秤座
            (
                $in_sign_id == 7
                &&
                (
                    ($in_sign_deg >= 28 && $in_sign_deg < 30)
                    || ($in_sign_deg >= 0 && $in_sign_deg < 6)
                )
            )
            ||
            // 天蝎座
            (
                $in_sign_id == 8
                &&
                (
                    ($in_sign_deg >= 0 && $in_sign_deg < 7)
                    || ($in_sign_deg >= 24 && $in_sign_deg < 30)
                )
            )
            ||
            // 射手座
            (
                $in_sign_id == 9
                &&
                (
                    ($in_sign_deg >= 26 && $in_sign_deg < 30)
                    || ($in_sign_deg >= 21 && $in_sign_deg < 26)
                )
            )
            ||
            // 摩羯座
            (
                $in_sign_id == 10
                &&
                (
                    ($in_sign_deg >= 26 && $in_sign_deg < 30)
                    || ($in_sign_deg >= 22 && $in_sign_deg < 26)
                )
            )
            ||
            // 水瓶座
            (
                $in_sign_id == 11
                &&
                (
                    ($in_sign_deg >= 20 && $in_sign_deg < 25)
                    || ($in_sign_deg >= 25 && $in_sign_deg < 30)
                )
            )
            ||
            // 双鱼座
            (
                $in_sign_id == 12
                &&
                (
                    ($in_sign_deg >= 19 && $in_sign_deg < 28)
                    || ($in_sign_deg >= 28 && $in_sign_deg < 30)
                )
            )
        )
        {
            $sign_score -= 5;
        }
        ###################


        $in_house_id = $this->InWhichHouse($fort,$details['houses']);
        #########################
        if($in_house_id == 1 || $in_house_id == 10)
        {
            $house_score += 5;
        }
        elseif($in_house_id == 4 || $in_house_id == 7 || $in_house_id == 11)
        {
            $house_score += 4;
        }
        elseif($in_house_id == 2 || $in_house_id == 5)
        {
            $house_score += 3;
        }
        elseif($in_house_id == 9)
        {
            $house_score += 2;
        }
        elseif($in_house_id == 3)
        {
            $house_score += 1;
        }
        elseif($in_house_id == 12)
        {
            $house_score -= 5;
        }
        elseif($in_house_id == 6 || $in_house_id == 8)
        {
            $house_score -= 4;
        }
        #############################

        if(abs($sun - $fort) > 270)
        {
            if($sun > $fort) $dist = $fort + 360 - $sun;
            else $dist = $sun + 360 - $fort;
        }
        else
        {
            $dist = abs($sun - $fort);
        }
        $burn = 0;// 是否灼伤
        if($dist > 8.5 && $dist <= 15) $burn = 1; // 在日光下 15
        if($dist > 17/60 && $dist <= 8.5) $burn = 2; // 灼伤 8.5
        #########################################
        foreach ($details['planets'] as $i => $p)
        {
            if(in_array($i,[3,4,5,6,12]))
            {
                $p_deg = $p['deg'];

                $da = abs($p_deg - $fort);
                if ($da > 180) $da = 360 - $da;

                $phases = [0,60,90,120,180];

                foreach ($phases as $ps)
                {
                    if($da <= ( $ps + $asp_orb_all[$ps]) && $da >= ( $ps - $asp_orb_all[$ps]))
                    {
                        if($i == 3 || $i == 5)
                        {
                            if($ps == 0) $phase_score += 5;
                            elseif($ps == 120) $phase_score += 4;
                            elseif($ps == 60) $phase_score += 3;
                        }
                        if($i == 12)
                        {
                            if($ps == 0) $phase_score += 3;
                            elseif($ps == 180) $phase_score -= 3;
                        }

                        if($i == 4 || $i == 6)
                        {
                            if($ps == 180) $phase_score -= 4;
                            elseif($ps == 0) $phase_score -= 6;
                            elseif($ps == 60 || $ps == 120) $phase_score -= 2;
                        }
                    }
                }
            }
        }
        if($burn != 1 && $burn != 2) $phase_score += 5;
        elseif($burn == 1) $phase_score -= 5;
        #########################################

        ###################################################################
        $diff = ($year *1 -2000)*0.83/60;
        // 计算恒星 每一百年移动83′ 每年0.83分
        foreach(config('astrolog.star') as $s)
        {
            $star = $s[0] + $diff;
            // 合恒星
            if(abs($fort-$star) <= 1)
            {
                // 吉凶状态  凶 平 吉 大吉
                if($s[1] == '轩辕十四') $star_score += 6;
                elseif($s[1] == '角宿一') $star_score += 5;
                elseif($s[1] == '大陵五') $star_score -= 6;
                elseif($s[6] == '吉' || $s[6] == '大吉') $star_score += 4;
                elseif($s[6] == '凶') $star_score -= 4;

            }
            // 刑恒星
            if(abs($fort-$star) <= 91 && abs($fort-$star) >= 89)
            {
                if($s[6] == '凶') $star_score -= 3;
            }
            // 冲恒星
            if(abs($fort-$star) <= 181 && abs($fort-$star) >= 179)
            {
                if($s[6] == '凶') $star_score -= 4;
            }
        }
        ###################################################################
        $total = $sign_score + $house_score + $phase_score + $star_score;
        $ret = ['ecliptic'=>$sign_score,'house'=>$house_score,'phase'=>$phase_score,'star'=>$star_score,'total'=>$total];
//        p($ret);exit;
        return $ret;
    }
    /**
     * 行星落宫
     * @param int 1-12
     *
     */
    private function InWhichHouse($longitude = 0,$house = array())
    {
        $house[13]['deg'] = $house[1]['deg'];
        $in_house = 12;
        for ($j=1; $j <= 12; $j++)
        {
            $pl = $longitude + (1 / 36000);
            $h_now = $house[$j]['deg'];
            $h_next = $house[$j+1]['deg'];

            if ($j < 12 && $h_now > $h_next)
                if (($pl >= $h_now && $pl < 360) || ($pl < $h_next && $pl >= 0)) $in_house = $j;
            if ($j == 12 && ($h_now > $house[1]['deg']))
                if (($pl >= $h_now && $pl < 360) || ($pl < $house[1]['deg'] && $pl >= 0)) $in_house = $j;
            if (($pl >= $h_now) && ($pl < $h_next && ($j < 12))) $in_house = $j;
            if (($pl >= $h_now) && ($pl < $house[1]['deg']) && ($j == 12)) $in_house = $j;
        }
        return $in_house;
    }
    /**
     * 获取行星结构
     * @param array $base_data
     * @return array
     */
    public function get_structure($base_data = [])
    {
        if(empty($base_data)) return [];
        $base_data['structure'] = $this->aspect_patterns($base_data);
        $base_data['structure']['asterism'] = (object)$this->asterism($base_data);
        $base_data['structure']['midpoint'] = $this->planet_midpoint($base_data);
        $base_data['structure']['finger'] = $this->god_finger($base_data);
        return $base_data;
    }
    /**
     * 大三角
     * 120°-120°-120°
     * 小三角 与两颗星都成60度的这颗星称为顶星
     * 120°-60°-60°
     * T三角 与两星成90度的星体称为“T顶点”
     * 180°-90°-90°
     * 桥三角：两颗星180度，同时有第三颗星与两星分别成60度和120度。该星称为桥星。
     * 180°-60°-120°
     * 大十字：四颗星组成一个正四方形，每颗星都与另外两颗成90度（可以认为是在T三角基础上，加上一颗与T顶点对冲的行星）
     * 180°-90°-90°-180°
     * 大信封：桥三角的桥星对面多出一颗对冲的行星
     * 大风筝：大三角顶星的对面，多出一颗与它成180度的行星
     */
    protected function aspect_patterns($base_data = [])
    {
        $ret = [
            'trine'=>[],
            'small_triangle'=>[],
            'kite'=>[],
            't_square'=>[],
            'cross'=>[],
            'bridge_triangle'=>[],
            'envelop'=>[],
        ];
        for($i = 0; $i < 10; $i ++)
        {
            $a = $base_data['planets'][$i];
            $phasei = $a['phase'];
            for($j = 0; $j < 10; $j ++)
            {
                $b = $base_data['planets'][$j];
                $phasej = $b['phase'];
                // 大三角 + 小三角
                if (isset($phasei[$i . "-" . $j]) && $phasei[$i . "-" . $j]['phase_e'] == 120 && $i < $j)
                {
                    for($z = 0; $z < 10; $z ++)
                    {
                        $c = $base_data['planets'][$z];
                        // 大三角
                        if (isset($phasei[$i . "-" . $z]) && $phasei[$i . "-" . $z]['phase_e'] == 120 && $j < $z
                            && isset($phasej[$j . "-" . $z]) && $phasej[$j . "-" . $z]['phase_e'] == 120 )
                        {
                            $ret['trine'][] = [
                                [
                                    'id'=>$i,
                                    'in_sign_id'=>$a['in_sign_id'],
                                    'in_house_id'=>$a['in_house_id'],
                                ],
                                [
                                    'id'=>$j,
                                    'in_sign_id'=>$b['in_sign_id'],
                                    'in_house_id'=>$b['in_house_id'],
                                ],
                                [
                                    'id'=>$z,
                                    'in_sign_id'=>$c['in_sign_id'],
                                    'in_house_id'=>$c['in_house_id'],
                                ],
                            ];
//                            $ret['trine'][] = [
//                                'id1'=>$i,
////                                'name1'=>$a['name'],
//                                'in_sign_id1'=>$a['in_sign_id'],
//                                'in_house_id1'=>$a['in_house_id'],
//                                'id2'=>$j,
////                                'name2'=>$b['name'],
//                                'in_sign_id2'=>$b['in_sign_id'],
//                                'in_house_id2'=>$b['in_house_id'],
//                                'id3'=>$z,
////                                'name3'=>$c['name'],
//                                'in_sign_id3'=>$c['in_sign_id'],
//                                'in_house_id3'=>$c['in_house_id'],
////                                'output'=>'大三角：'
////                                    .$a['name'].'（'.mb_substr($a['in_sign_whole'],0,2,'utf-8'). ' '.$a['in_house_id'] .'宫）+'
////                                    .$b['name'].'（'.mb_substr($b['in_sign_whole'],0,2,'utf-8'). ' '.$b['in_house_id'] .'宫）+【'
////                                    .$c['name'].'】（'.mb_substr($c['in_sign_whole'],0,2,'utf-8'). ' '.$c['in_house_id'] .'宫）'
//                            ];
                            for ($y=0; $y < 10;$y++)
                            {
                                $d = $base_data['planets'][$y];
                                $phasey = $d['phase'];
                                // 大风筝：大三角顶星的对面，多出一颗与它成180度的行星
                                if (isset($phasey[$y . "-" . $z]) && $phasey[$y . "-" . $z]['phase_e'] == 180)
                                {
                                    $ret['kite'][] = [
                                        [
                                            'id'=>$i,
                                            'in_sign_id'=>$a['in_sign_id'],
                                            'in_house_id'=>$a['in_house_id'],
                                            'top'=>false,
                                        ],
                                        [
                                            'id'=>$j,
                                            'in_sign_id'=>$b['in_sign_id'],
                                            'in_house_id'=>$b['in_house_id'],
                                            'top'=>false,
                                        ],
                                        [
                                            'id'=>$z,
                                            'in_sign_id'=>$c['in_sign_id'],
                                            'in_house_id'=>$c['in_house_id'],
                                            'top'=>false,
                                        ],
                                        [
                                            'id'=>$y,
                                            'in_sign_id'=>$d['in_sign_id'],
                                            'in_house_id'=>$d['in_house_id'],
                                            'top'=>true,
                                        ],
                                    ];
//                                    $ret['kite'][] = [
//                                        'id1' => $i,
////                                        'name1' => $a['name'],
//                                        'in_sign_id1' => $a['in_sign_id'],
//                                        'in_house_id1' => $a['in_house_id'],
//                                        'id2' => $j,
////                                        'name2' => $b['name'],
//                                        'in_sign_id2' => $b['in_sign_id'],
//                                        'in_house_id2' => $b['in_house_id'],
//                                        'id3' => $z,
////                                        'name3' => $c['name'],
//                                        'in_sign_id3' => $c['in_sign_id'],
//                                        'in_house_id3' => $c['in_house_id'],
//                                        'id4' => $y,
////                                        'name4' => $d['name'],
//                                        'in_sign_id4' => $d['in_sign_id'],
//                                        'in_house_id4' => $d['in_house_id'],
//                                        'top'=>$y,
////                                        'output' => '大风筝：'
////                                            . $a['name'] . '（' . mb_substr($a['in_sign_whole'],0,2,'utf-8') . ' ' . $a['in_house_id'] . '宫）+'
////                                            . $b['name'] . '（' . mb_substr($b['in_sign_whole'],0,2,'utf-8') . ' ' . $b['in_house_id'] . '宫）+'
////                                            . $c['name'] . '（' . mb_substr($c['in_sign_whole'],0,2,'utf-8') . ' ' . $c['in_house_id'] . '宫）【+'
////                                            . $d['name'] . '】（' . mb_substr($d['in_sign_whole'],0,2,'utf-8') . ' ' . $d['in_house_id'] . '宫）+'
//                                    ];
                                }
                            }
                        }
                        // 小三角
                        elseif(isset($phasei[$i . "-" . $z]) && $phasei[$i . "-" . $z]['phase_e'] == 60 && $j < $z
                            && isset($phasej[$j . "-" . $z]) && $phasej[$j . "-" . $z]['phase_e'] == 60 )
                        {
                            $ret['small_triangle'][] = [
                                [
                                    'id'=>$i,
                                    'in_sign_id'=>$a['in_sign_id'],
                                    'in_house_id'=>$a['in_house_id'],
                                    'top'=>false,
                                ],
                                [
                                    'id'=>$j,
                                    'in_sign_id'=>$b['in_sign_id'],
                                    'in_house_id'=>$b['in_house_id'],
                                    'top'=>false,
                                ],
                                [
                                    'id'=>$z,
                                    'in_sign_id'=>$c['in_sign_id'],
                                    'in_house_id'=>$c['in_house_id'],
                                    'top'=>true,
                                ]
                            ];
//                            $ret['small_triangle'][] = [
//                                'id1'=>$i,
////                                'name1'=>$a['name'],
//                                'in_sign_id1'=>$a['in_sign_id'],
//                                'in_house_id1'=>$a['in_house_id'],
//                                'id2'=>$j,
////                                'name2'=>$b['name'],
//                                'in_sign_id2'=>$b['in_sign_id'],
//                                'in_house_id2'=>$b['in_house_id'],
//                                'id3'=>$z,
////                                'name3'=>$c['name'],
//                                'in_sign_id3'=>$c['in_sign_id'],
//                                'in_house_id3'=>$c['in_house_id'],
//                                'top'=>$z,
////                                'output'=>'小三角：'
////                                    .$a['name'].'（'.mb_substr($a['in_sign_whole'],0,2,'utf-8'). ' '.$a['in_house_id'] .'宫）+'
////                                    .$b['name'].'（'.mb_substr($b['in_sign_whole'],0,2,'utf-8'). ' '.$b['in_house_id'] .'宫）+【'
////                                    .$c['name'].'】（'.mb_substr($c['in_sign_whole'],0,2,'utf-8'). ' '.$c['in_house_id'] .'宫）'
//                            ];
                        }

                    }
                }
                // T三角 + 大十字
                elseif(isset($phasei[$i . "-" . $j]) && $phasei[$i . "-" . $j]['phase_e'] == 180 && $i < $j )
                {
                    for($z = 0; $z < 10; $z ++) {
                        $c = $base_data['planets'][$z];
                        $phasez = $c['phase'];
                        // T三角
                        if (isset($phasei[$i . "-" . $z]) && $phasei[$i . "-" . $z]['phase_e'] == 90 && $i != $z
                            && isset($phasej[$j . "-" . $z]) && $phasej[$j . "-" . $z]['phase_e'] == 90 && $j != $z
                        ) {
                            $ret['t_square'][] = [
                                [
                                    'id'=>$i,
                                    'in_sign_id'=>$a['in_sign_id'],
                                    'in_house_id'=>$a['in_house_id'],
                                    'top'=>false,
                                ],
                                [
                                    'id'=>$j,
                                    'in_sign_id'=>$b['in_sign_id'],
                                    'in_house_id'=>$b['in_house_id'],
                                    'top'=>false,
                                ],
                                [
                                    'id'=>$z,
                                    'in_sign_id'=>$c['in_sign_id'],
                                    'in_house_id'=>$c['in_house_id'],
                                    'top'=>true,
                                ]
                            ];
//                            $ret['t_square'][] = [
//                                'id1' => $i,
////                                'name1' => $a['name'],
//                                'in_sign_id1' => $a['in_sign_id'],
//                                'in_house_id1' => $a['in_house_id'],
//                                'id2' => $j,
////                                'name2' => $b['name'],
//                                'in_sign_id2' => $b['in_sign_id'],
//                                'in_house_id2' => $b['in_house_id'],
//                                'id3' => $z,
////                                'name3' => $c['name'],
//                                'in_sign_id3' => $c['in_sign_id'],
//                                'in_house_id3' => $c['in_house_id'],
//                                'top'=>$z,
////                                'output' => 'T三角：'
////                                    . $a['name'] . '（' . mb_substr($a['in_sign_whole'],0,2,'utf-8') . ' ' . $a['in_house_id'] . '宫）+'
////                                    . $b['name'] . '（' . mb_substr($b['in_sign_whole'],0,2,'utf-8') . ' ' . $b['in_house_id'] . '宫）+【'
////                                    . $c['name'] . '】（' . mb_substr($c['in_sign_whole'],0,2,'utf-8') . ' ' . $c['in_house_id'] . '宫）'
//                            ];
                            for ($y=0; $y < 10;$y++)
                            {
                                $d = $base_data['planets'][$y];
                                $phasey = $d['phase'];

                                // 大十字
                                if(isset($phasey[$y . "-" . $z]) && $phasey[$y . "-" . $z]['phase_e'] == 180 && $i < $z
                                    && isset($phasey[$y . "-" . $j]) && $phasey[$y . "-" . $j]['phase_e'] == 90
                                    && isset($phasey[$y . "-" . $i]) && $phasey[$y . "-" . $i]['phase_e'] == 90 && $z < $y
                                )
                                {
                                    $ret['cross'][] = [
                                        [
                                            'id'=>$i,
                                            'in_sign_id'=>$a['in_sign_id'],
                                            'in_house_id'=>$a['in_house_id'],
                                            'top'=>false,
                                        ],
                                        [
                                            'id'=>$j,
                                            'in_sign_id'=>$b['in_sign_id'],
                                            'in_house_id'=>$b['in_house_id'],
                                            'top'=>false,
                                        ],
                                        [
                                            'id'=>$z,
                                            'in_sign_id'=>$c['in_sign_id'],
                                            'in_house_id'=>$c['in_house_id'],
                                            'top'=>false,
                                        ],
                                        [
                                            'id'=>$y,
                                            'in_sign_id'=>$d['in_sign_id'],
                                            'in_house_id'=>$d['in_house_id'],
                                            'top'=>true,
                                        ],
                                    ];
//                                    $ret['cross'][] = [
//                                        'id1' => $i,
////                                        'name1' => $a['name'],
//                                        'in_sign_id1' => $a['in_sign_id'],
//                                        'in_house_id1' => $a['in_house_id'],
//                                        'id2' => $j,
////                                        'name2' => $b['name'],
//                                        'in_sign_id2' => $b['in_sign_id'],
//                                        'in_house_id2' => $b['in_house_id'],
//                                        'id3' => $z,
////                                        'name3' => $c['name'],
//                                        'in_sign_id3' => $c['in_sign_id'],
//                                        'in_house_id3' => $c['in_house_id'],
//                                        'id4' => $y,
////                                        'name4' => $d['name'],
//                                        'in_sign_id4' => $d['in_sign_id'],
//                                        'in_house_id4' => $d['in_house_id'],
////                                        'output' => '大十字：'
////                                            . $a['name'] . '（' . mb_substr($a['in_sign_whole'],0,2,'utf-8') . ' ' . $a['in_house_id'] . '宫）+'
////                                            . $b['name'] . '（' . mb_substr($b['in_sign_whole'],0,2,'utf-8') . ' ' . $b['in_house_id'] . '宫）+'
////                                            . $c['name'] . '（' . mb_substr($c['in_sign_whole'],0,2,'utf-8') . ' ' . $c['in_house_id'] . '宫）+'
////                                            . $d['name'] . '（' . mb_substr($d['in_sign_whole'],0,2,'utf-8') . ' ' . $d['in_house_id'] . '宫）+'
//                                    ];
                                }
                            }
                        }
                        // 桥三角 两颗星180度，同时有第三颗星与两星分别成60度和120度。该星称为桥星。
                        // 大信封：桥三角的桥星对面多出一颗对冲的行星
                        elseif ($i != $z && $j != $z &&
                            ((isset($phasez[$z . "-" . $i]) && $phasez[$z . "-" . $i]['phase_e'] == 120
                                    && isset($phasez[$z . "-" . $j]) && $phasez[$z . "-" . $j]['phase_e'] == 60)
                                || (isset($phasez[$z . "-" . $i]) && $phasez[$z . "-" . $i]['phase_e'] == 60
                                    && isset($phasez[$z . "-" . $j]) && $phasez[$z . "-" . $j]['phase_e'] == 120))
                        )
                        {
                            $ret['bridge_triangle'][] = [
                                [
                                    'id'=>$i,
                                    'in_sign_id'=>$a['in_sign_id'],
                                    'in_house_id'=>$a['in_house_id'],
                                    'top'=>false,
                                ],
                                [
                                    'id'=>$j,
                                    'in_sign_id'=>$b['in_sign_id'],
                                    'in_house_id'=>$b['in_house_id'],
                                    'top'=>false,
                                ],
                                [
                                    'id'=>$z,
                                    'in_sign_id'=>$c['in_sign_id'],
                                    'in_house_id'=>$c['in_house_id'],
                                    'top'=>true,
                                ],
                            ];
//                            $ret['bridge_triangle'][] = [
//                                'id1'=>$i,
////                                'name1'=>$a['name'],
//                                'in_sign_id1'=>$a['in_sign_id'],
//                                'in_house_id1'=>$a['in_house_id'],
//                                'id2'=>$j,
////                                'name2'=>$b['name'],
//                                'in_sign_id2'=>$b['in_sign_id'],
//                                'in_house_id2'=>$b['in_house_id'],
//                                'id3'=>$z,
////                                'name3'=>$c['name'],
//                                'in_sign_id3'=>$c['in_sign_id'],
//                                'in_house_id3'=>$c['in_house_id'],
//                                'top'=>$z,
////                                'output'=>'桥三角：'
////                                    .$a['name'].'（'.mb_substr($a['in_sign_whole'],0,2,'utf-8'). ' '.$a['in_house_id'] .'宫）+'
////                                    .$b['name'].'（'.mb_substr($b['in_sign_whole'],0,2,'utf-8'). ' '.$b['in_house_id'] .'宫）+【'
////                                    .$c['name'].'】（'.mb_substr($c['in_sign_whole'],0,2,'utf-8'). ' '.$c['in_house_id'] .'宫）'
//                            ];
                            for ($y=0;$y<10;$y++)
                            {
                                $d = $base_data['planets'][$y];
                                $phasey = $d['phase'];
                                // 大信封
                                if (isset($phasey[$y . "-" . $z]) && $phasey[$y . "-" . $z]['phase_e'] == 180
                                    && $z < $y && $i< $z
                                )
                                {
                                    $ret['envelop'][] = [
                                        [
                                            'id'=>$i,
                                            'in_sign_id'=>$a['in_sign_id'],
                                            'in_house_id'=>$a['in_house_id'],
                                        ],
                                        [
                                            'id'=>$j,
                                            'in_sign_id'=>$b['in_sign_id'],
                                            'in_house_id'=>$b['in_house_id'],
                                        ],
                                        [
                                            'id'=>$z,
                                            'in_sign_id'=>$c['in_sign_id'],
                                            'in_house_id'=>$c['in_house_id'],
                                        ],
                                        [
                                            'id'=>$y,
                                            'in_sign_id'=>$d['in_sign_id'],
                                            'in_house_id'=>$d['in_house_id'],
                                        ],
                                    ];
//                                    $ret['envelop'][] = [
//                                        'id1' => $i,
////                                        'name1' => $a['name'],
//                                        'in_sign_id1' => $a['in_sign_id'],
//                                        'in_house_id1' => $a['in_house_id'],
//                                        'id2' => $j,
////                                        'name2' => $b['name'],
//                                        'in_sign_id2' => $b['in_sign_id'],
//                                        'in_house_id2' => $b['in_house_id'],
//                                        'id3' => $z,
////                                        'name3' => $c['name'],
//                                        'in_sign_id3' => $c['in_sign_id'],
//                                        'in_house_id3' => $c['in_house_id'],
//                                        'id4' => $y,
////                                        'name4' => $d['name'],
//                                        'in_sign_id4' => $d['in_sign_id'],
//                                        'in_house_id4' => $d['in_house_id'],
////                                        'output' => '大信封：'
////                                            . $a['name'] . '（' . mb_substr($a['in_sign_whole'],0,2,'utf-8') . ' ' . $a['in_house_id'] . '宫）+'
////                                            . $b['name'] . '（' . mb_substr($b['in_sign_whole'],0,2,'utf-8') . ' ' . $b['in_house_id'] . '宫）+'
////                                            . $c['name'] . '（' . mb_substr($c['in_sign_whole'],0,2,'utf-8') . ' ' . $c['in_house_id'] . '宫）+'
////                                            . $d['name'] . '（' . mb_substr($d['in_sign_whole'],0,2,'utf-8') . ' ' . $d['in_house_id'] . '宫）+'
//                                    ];
                                }
                            }
                        }
                    }
                }
            }
        }
        return $ret;
    }
    /**
     * 星群：三颗以上行星聚拢，称作星群（不计算小行星）
     * 紧密星群：三颗以上行星，彼此合相
     * 疏松星群：三颗以上行星，不满足紧密星群条件，但每颗星都有一个合相
     * 分散星群：三颗以上行星，不满足前两种，但位于同一星座或同一宫位
     */
    protected function asterism($base_data = [])
    {
        $ret = [];
        $signs_arr = [];
        $houses_arr = [];
        for($i=0;$i<10;$i++)
        {
            $signs_arr[$base_data['planets'][$i]['in_sign_id']][] = $i;
            $houses_arr[$base_data['planets'][$i]['in_house_id']][] = $i;
        }
        $check = [];
        for($i = 0; $i < 10; $i ++)
        {
            $a = $base_data['planets'][$i];

            $phasei = $a['phase'];
            for($j = 0; $j < 10; $j ++)
            {
                $b = $base_data['planets'][$j];
                $phasej = $b['phase'];
                if(isset($phasei[$i . "-" . $j]) && $phasei[$i . "-" . $j]['phase_e'] == 0 && $i < $j)
                {
                    for($k=0;$k<10;$k++)
                    {
                        $c = $base_data['planets'][$k];
                        $phasek = $c['phase'];
                        // 紧密星群：三颗以上行星，彼此合相
                        if(isset($phasei[$i . "-" . $k]) && $phasei[$i . "-" . $k]['phase_e'] == 0 && $j < $k
                            && isset($phasej[$j . "-" . $k]) && $phasej[$j . "-" . $k]['phase_e'] == 0
                        )
                        {
                            $check_str = $i.'-'.$j.'-'.$k;
                            if(!$this->check_in($check_str,$check))
                            {
                                $check[] = $check_str;
                                $ret[1][] = [
                                    [
                                        'id'=>$i,
                                        'in_sign_id'=>$a['in_sign_id'],
                                        'in_house_id'=>$a['in_house_id'],
                                    ],
                                    [
                                        'id'=>$j,
                                        'in_sign_id'=>$b['in_sign_id'],
                                        'in_house_id'=>$b['in_house_id'],
                                    ],
                                    [
                                        'id'=>$k,
                                        'in_sign_id'=>$c['in_sign_id'],
                                        'in_house_id'=>$c['in_house_id'],
                                    ],
                                ];
//                                $ret[] = [
//                                    'id1'=>$i,
////                                    'name1'=>$a['name'],
//                                    'in_sign_id1'=>$a['in_sign_id'],
//                                    'in_house_id1'=>$a['in_house_id'],
//                                    'id2'=>$j,
////                                    'name2'=>$b['name'],
//                                    'in_sign_id2'=>$b['in_sign_id'],
//                                    'in_house_id2'=>$b['in_house_id'],
//                                    'id3'=>$k,
////                                    'name3'=>$c['name'],
//                                    'in_sign_id3'=>$c['in_sign_id'],
//                                    'in_house_id3'=>$c['in_house_id'],
//                                    'num'=>3,
//                                    'status'=>1,
////                                    'output'=>'紧密星群：'
////                                        .$a['name'].'（'.mb_substr($a['in_sign_whole'],0,2,'utf-8'). ' '.$a['in_house_id'] .'宫）+'
////                                        .$b['name'].'（'.mb_substr($b['in_sign_whole'],0,2,'utf-8'). ' '.$b['in_house_id'] .'宫）+'
////                                        .$c['name'].'（'.mb_substr($c['in_sign_whole'],0,2,'utf-8'). ' '.$c['in_house_id'] .'宫）'
//                                ];
                            }

                        }
                        // 疏松星群：三颗以上行星，不满足紧密星群条件，但每颗星都有一个合相
                        elseif($j < $k &&
                            ((isset($phasei[$i . "-" . $k]) && $phasei[$i . "-" . $k]['phase_e'] == 0)
                                || (isset($phasej[$j . "-" . $k]) && $phasej[$j . "-" . $k]['phase_e'] == 0))
                        )
                        {
                            $ss = 0;
                            for($g = 0;$g<10;$g++)
                            {
                                $d = $base_data['planets'][$g];
                                if($k<$g &&
                                    ((isset($phasei[$i . "-" . $g]) && $phasei[$i . "-" . $g]['phase_e'] == 0)
                                        || (isset($phasej[$j . "-" . $g]) && $phasej[$j . "-" . $g]['phase_e'] == 0)
                                        || (isset($phasek[$k . "-" . $g]) && $phasek[$k . "-" . $g]['phase_e'] == 0))
                                )
                                {
                                    $check_str = $i.'-'.$j.'-'.$k.'-'.$g;
                                    if(!$this->check_in($check_str,$check))
                                    {
                                        $check[] = $check_str;
                                        $ss = 1;
                                        $ret[2][] = [
                                            [
                                                'id'=>$i,
                                                'in_sign_id'=>$a['in_sign_id'],
                                                'in_house_id'=>$a['in_house_id'],
                                            ],
                                            [
                                                'id'=>$j,
                                                'in_sign_id'=>$b['in_sign_id'],
                                                'in_house_id'=>$b['in_house_id'],
                                            ],
                                            [
                                                'id'=>$k,
                                                'in_sign_id'=>$c['in_sign_id'],
                                                'in_house_id'=>$c['in_house_id'],
                                            ],
                                            [
                                                'id'=>$g,
                                                'in_sign_id'=>$d['in_sign_id'],
                                                'in_house_id'=>$d['in_house_id'],
                                            ],
                                        ];
//                                        $ret[] = [
//                                            'id1'=>$i,
////                                            'name1'=>$a['name'],
//                                            'in_sign_id1'=>$a['in_sign_id'],
//                                            'in_house_id1'=>$a['in_house_id'],
//                                            'id2'=>$j,
////                                            'name2'=>$b['name'],
//                                            'in_sign_id2'=>$b['in_sign_id'],
//                                            'in_house_id2'=>$b['in_house_id'],
//                                            'id3'=>$k,
////                                            'name3'=>$c['name'],
//                                            'in_sign_id3'=>$c['in_sign_id'],
//                                            'in_house_id3'=>$c['in_house_id'],
//                                            'id4'=>$g,
////                                            'name4'=>$d['name'],
//                                            'in_sign_id4'=>$d['in_sign_id'],
//                                            'in_house_id4'=>$d['in_house_id'],
//                                            'num'=>4,
//                                            'status'=>2,
////                                            'output'=>'疏松星群：'
////                                                .$a['name'].'（'.mb_substr($a['in_sign_whole'],0,2,'utf-8'). ' '.$a['in_house_id'] .'宫）+'
////                                                .$b['name'].'（'.mb_substr($b['in_sign_whole'],0,2,'utf-8'). ' '.$b['in_house_id'] .'宫）+'
////                                                .$c['name'].'（'.mb_substr($c['in_sign_whole'],0,2,'utf-8'). ' '.$c['in_house_id'] .'宫）+'
////                                                .$d['name'].'（'.mb_substr($d['in_sign_whole'],0,2,'utf-8'). ' '.$d['in_house_id'] .'宫）'
//                                        ];
                                    }
                                }
                            }
                            if($ss == 0)
                            {
                                $check_str = $i.'-'.$j.'-'.$k;
                                if(!$this->check_in($check_str,$check)) {
                                    $check[] = $check_str;
                                    $ret[2][] = [
                                        [
                                            'id'=>$i,
                                            'in_sign_id'=>$a['in_sign_id'],
                                            'in_house_id'=>$a['in_house_id'],
                                        ],
                                        [
                                            'id'=>$j,
                                            'in_sign_id'=>$b['in_sign_id'],
                                            'in_house_id'=>$b['in_house_id'],
                                        ],
                                        [
                                            'id'=>$k,
                                            'in_sign_id'=>$c['in_sign_id'],
                                            'in_house_id'=>$c['in_house_id'],
                                        ],
                                    ];
//                                    $ret[] = [
//                                        'id1' => $i,
////                                        'name1' => $a['name'],
//                                        'in_sign_id1' => $a['in_sign_id'],
//                                        'in_house_id1' => $a['in_house_id'],
//                                        'id2' => $j,
////                                        'name2' => $b['name'],
//                                        'in_sign_id2' => $b['in_sign_id'],
//                                        'in_house_id2' => $b['in_house_id'],
//                                        'id3' => $k,
////                                        'name3' => $c['name'],
//                                        'in_sign_id3' => $c['in_sign_id'],
//                                        'in_house_id3' => $c['in_house_id'],
//                                        'num'=>3,
//                                        'status' => 2,
////                                        'output' => '疏松星群：'
////                                            . $a['name'] . '（' . mb_substr($a['in_sign_whole'], 0, 2, 'utf-8') . ' ' . $a['in_house_id'] . '宫）+'
////                                            . $b['name'] . '（' . mb_substr($b['in_sign_whole'], 0, 2, 'utf-8') . ' ' . $b['in_house_id'] . '宫）+'
////                                            . $c['name'] . '（' . mb_substr($c['in_sign_whole'], 0, 2, 'utf-8') . ' ' . $c['in_house_id'] . '宫）'
//                                    ];
                                }
                            }
                        }
                    }
                }
            }
        }
        // 分散星群 ：三颗或以上行星，不满足前两种，但位于同一星座或同一宫位
        for($i_i=1;$i_i<13;$i_i++)
        {
            $c_s = (isset($signs_arr[$i_i]) && count($signs_arr[$i_i])) ? count($signs_arr[$i_i]) : 0;
            $c_h = (isset($houses_arr[$i_i]) && count($houses_arr[$i_i])) ? count($houses_arr[$i_i]) : 0;
            // 位于同一星座
            if($c_s >= 3)
            {
                $a = $base_data['planets'][$signs_arr[$i_i][0]];
                $b = $base_data['planets'][$signs_arr[$i_i][1]];
                $c = $base_data['planets'][$signs_arr[$i_i][2]];
                $data = [
                    [
                        'id'=>$signs_arr[$i_i][0],
                        'in_sign_id'=>$a['in_sign_id'],
                        'in_house_id'=>$a['in_house_id'],
                    ],
                    [
                        'id'=>$signs_arr[$i_i][1],
                        'in_sign_id'=>$b['in_sign_id'],
                        'in_house_id'=>$b['in_house_id'],
                    ],
                    [
                        'id'=>$signs_arr[$i_i][2],
                        'in_sign_id'=>$c['in_sign_id'],
                        'in_house_id'=>$c['in_house_id'],
                    ]
                ];
//                $data = [
//                    'id1'=>$signs_arr[$i_i][0],
////                    'name1'=>$a['name'],
//                    'in_sign_id1'=>$a['in_sign_id'],
//                    'in_house_id1'=>$a['in_house_id'],
//                    'id2'=>$signs_arr[$i_i][1],
////                    'name2'=>$b['name'],
//                    'in_sign_id2'=>$b['in_sign_id'],
//                    'in_house_id2'=>$b['in_house_id'],
//                    'id3'=>$signs_arr[$i_i][2],
////                    'name3'=>$c['name'],
//                    'in_sign_id3'=>$c['in_sign_id'],
//                    'in_house_id3'=>$c['in_house_id'],
//                    'num'=>$c_s,
//                    'status'=>3,
////                    'output'=>'分散星群：'
////                        .$a['name'].'（'.mb_substr($a['in_sign_whole'],0,2,'utf-8'). ' '.$a['in_house_id'] .'宫）+'
////                        .$b['name'].'（'.mb_substr($b['in_sign_whole'],0,2,'utf-8'). ' '.$b['in_house_id'] .'宫）+'
////                        .$c['name'].'（'.mb_substr($c['in_sign_whole'],0,2,'utf-8'). ' '.$c['in_house_id'] .'宫）'
//                ];
                $check_str = $signs_arr[$i_i][0].'-'.$signs_arr[$i_i][1].'-'.$signs_arr[$i_i][2];
                if($c_s > 3)
                {
                    for($s=3;$s<$c_s;$s++)
                    {
                        $check_str .= '-'.$signs_arr[$i_i][$s];
                        $temp = $base_data['planets'][$signs_arr[$i_i][$s]];
                        $data[] = [
                            'id'=>$signs_arr[$i_i][$s],
                            'in_sign_id'=>$temp['in_sign_id'],
                            'in_house_id'=>$temp['in_house_id'],
                        ];
//                        $data["id".($s+1)] = $signs_arr[$i_i][$s];
////                        $data["name".($s+1)] = $temp['name'];
//                        $data["in_sign_id".($s+1)] = $temp['in_sign_id'];
//                        $data["in_house_id".($s+1)] = $temp['in_house_id'];
//                        $data['output'] .= '+' . $temp['name'].'（'.mb_substr($temp['in_sign_whole'],0,2,'utf-8'). ' '.$temp['in_house_id'] .'宫）';
                    }
                }
                if(!$this->check_in($check_str,$check))
                {
                    $data = $this->reset_sort($check_str,$data,'in_house_id',$base_data);
                    // 同一星座的  按照宫位id排序
                    $check[] = $check_str;
                    $ret[3][] = $data;
                    unset($signs_arr[$i_i]);
                }
            }
            // 位于同一宫位
            if($c_h >= 3)
            {
                $a = $base_data['planets'][$houses_arr[$i_i][0]];
                $b = $base_data['planets'][$houses_arr[$i_i][1]];
                $c = $base_data['planets'][$houses_arr[$i_i][2]];
                $data = [
                    [
                        'id'=>$houses_arr[$i_i][0],
                        'in_sign_id'=>$a['in_sign_id'],
                        'in_house_id'=>$a['in_house_id'],
                    ],
                    [
                        'id'=>$houses_arr[$i_i][1],
                        'in_sign_id'=>$b['in_sign_id'],
                        'in_house_id'=>$b['in_house_id'],
                    ],
                    [
                        'id'=>$houses_arr[$i_i][2],
                        'in_sign_id'=>$c['in_sign_id'],
                        'in_house_id'=>$c['in_house_id'],
                    ]
                ];
//                $data = [
//                    'id1'=>$houses_arr[$i_i][0],
////                    'name1'=>$a['name'],
//                    'in_sign_id1'=>$a['in_sign_id'],
//                    'in_house_id1'=>$a['in_house_id'],
//                    'id2'=>$houses_arr[$i_i][1],
////                    'name2'=>$b['name'],
//                    'in_sign_id2'=>$b['in_sign_id'],
//                    'in_house_id2'=>$b['in_house_id'],
//                    'id3'=>$houses_arr[$i_i][2],
////                    'name3'=>$c['name'],
//                    'in_sign_id3'=>$c['in_sign_id'],
//                    'in_house_id3'=>$c['in_house_id'],
//                    'num'=>$c_h,
//                    'status'=>3,
////                    'output'=>'分散星群：'
////                        .$a['name'].'（'.mb_substr($a['in_sign_whole'],0,2,'utf-8'). ' '.$a['in_house_id'] .'宫）+'
////                        .$b['name'].'（'.mb_substr($b['in_sign_whole'],0,2,'utf-8'). ' '.$b['in_house_id'] .'宫）+'
////                        .$c['name'].'（'.mb_substr($c['in_sign_whole'],0,2,'utf-8'). ' '.$c['in_house_id'] .'宫）'
//                ];
                $check_str = $houses_arr[$i_i][0].'-'.$houses_arr[$i_i][1].'-'.$houses_arr[$i_i][2];
                if($c_h > 3)
                {
                    for($s=3;$s<$c_h;$s++)
                    {
                        $check_str .= '-'.$houses_arr[$i_i][$s];
                        $temp = $base_data['planets'][$houses_arr[$i_i][$s]];
                        $data[] = [
                            'id'=>$houses_arr[$i_i][$s],
                            'in_sign_id'=>$temp['in_sign_id'],
                            'in_house_id'=>$temp['in_house_id'],
                        ];
//                        $data["id".($s+1)] = $houses_arr[$i_i][$s];
////                        $data["name".($s+1)] = $temp['name'];
//                        $data["in_sign_id".($s+1)] = $temp['in_sign_id'];
//                        $data["in_house_id".($s+1)] = $temp['in_house_id'];
//                        $data['output'] .= '+' . $temp['name'].'（'.mb_substr($temp['in_sign_whole'],0,2,'utf-8'). ' '.$temp['in_house_id'] .'宫）';
                    }
                }
                if(!$this->check_in($check_str,$check))
                {
                    $data = $this->reset_sort($check_str,$data,'in_sign_id',$base_data);
                    // 同一宫位的  按照星座id排序
                    unset($houses_arr[$i_i]);
                    $check[] = $check_str;
                    $ret[3][] = $data;

                }
            }
        }


        return $ret;
    }
    /**
     * 重新排序
     * @param string $str
     * @param array $data
     * @return array
     */
    protected function reset_sort($str = '',$data = [],$sort_by = 'deg',$base_data = [])
    {
        if($str == '') return [];
        foreach ($data as $k1 => $d1)
        {

            for($k2 = $k1+1;$k2<count($data);$k2++)
            {
                $d2 = $data[$k2];
                if($base_data['planets'][$d1['id']][$sort_by] > $base_data['planets'][$d2['id']][$sort_by])
                {
                    $temp = $d1;
                    $data[$k1] = $d2;
                    $data[$k2] = $temp;
                }
            }
        }
//        $str_arr = explode('-',$str);
//        for($i=0;$i<count($str_arr);$i++)
//        {
//            for($j=$i+1;$j<count($str_arr);$j++)
//            {
//                if($base_data['planets'][$str_arr[$i]][$sort_by] > $base_data['planets'][$str_arr[$j]][$sort_by])
//                {
//                    $tp =
//                    $id = $str_arr[$i];
//                    p($id);
////                    $name = $base_data['planets'][$str_arr[$i]]['name'];
//                    $in_sign_id = $base_data['planets'][$str_arr[$i]]['in_sign_id'];
//                    $in_house_id = $base_data['planets'][$str_arr[$i]]['in_house_id'];
////                    $data['id'.($i+1)] = $data['id'.($j+1)];
//////                    $data['name'.($i+1)] = $data['name'.($j+1)];
////                    $data['in_sign_id'.($i+1)] = $data['in_sign_id'.($j+1)];
////                    $data['in_house_id'.($i+1)] = $data['in_house_id'.($j+1)];
////                    $data['id'.($j+1)] = $id;
//////                    $data['name'.($j+1)] = $name;
////                    $data['in_sign_id'.($j+1)] = $in_sign_id;
////                    $data['in_house_id'.($j+1)] = $in_house_id;
//
//                    $temp = $str_arr[$i];
//                    $str_arr[$i] = $str_arr[$j];
//                    $str_arr[$j] = $temp;
//
//                }
//            }
//        }
//        $out_put = '分散星群：';
//        foreach ($str_arr as $k => $st)
//        {
//            $out_put .= $base_data['planets'][$st]['name'] .
//                '（'.mb_substr($base_data['planets'][$st]['in_sign_whole'],0,2,'utf-8'). ' '.$base_data['planets'][$st]['in_house_id'] .'宫）+';
//        }
//        $out_put = rtrim($out_put, "+");
//        $data['output'] = $out_put;
//        $new_str = implode('-',$str_arr);
        return $data;
    }
    /**
     * 计算行星中点(包括四轴)
     */
    protected function planet_midpoint($base_data = [])
    {
        $ret = [];
        for($i=0;$i<10;$i++)
        {
            $a = $base_data['planets'][$i];

            $deg1 = $a['deg'];
            for ($j=$i+1;$j<10;$j++)
            {
                $b = $base_data['planets'][$j];
                $deg2 = $b['deg'];
                $mid = ($deg1 + $deg2) /2 ;// 中点度数 (A+B)/2
                for($k=0;$k<10;$k++)
                {
                    $c = $base_data['planets'][$k];
                    $deg3 = $c['deg'];
                    $diff = abs($deg3 - $mid); // 计算相差度数
                    if($diff > 180) $diff = 360 - $diff;
                    // 判断是否与行星形成 0°/90°/180°相位
                    if($i != $k && $j != $k
                        && (($diff <= 1 ) || ($diff > 89 && $diff < 91) || ($diff > 179 && $diff < 181) ))
                    {
                        $ret[] = [
                            'id1'=>$i,
//                            'name1'=>$a['name'],
                            'in_sign_id1'=>$a['in_sign_id'],
                            'in_house_id1'=>$a['in_house_id'],
                            'id2'=>$j,
//                            'name2'=>$b['name'],
                            'in_sign_id2'=>$b['in_sign_id'],
                            'in_house_id2'=>$b['in_house_id'],
                            'id3'=>$k,
//                            'name3'=>$c['name'],
                            'in_sign_id3'=>$c['in_sign_id'],
                            'in_house_id3'=>$c['in_house_id'],
                            'asp'=>$diff,
//                            'output'=>$a['name_whole'].' + '.$b['name_whole'].' = '.$c['name_whole'],
                        ];
                    }
                }
                $asc = $base_data['houses'][1];
//                $ic = $base_data['houses'][4];
//                $des = $base_data['houses'][7];
                $mc = $base_data['houses'][10];
                $asc_deg = $asc['deg'];
//                $ic_deg = $ic['deg'];
//                $des_deg = $des['deg'];
                $mc_deg = $mc['deg'];
                $axis = [17=>$asc_deg,20=>$mc_deg];
                foreach($axis as $ak => $ax)
                {
                    $diff_axis = abs($ax - $mid); // 计算相差度数
                    if($diff_axis > 180) $diff_axis = 360 - $diff_axis;
                    if(($diff_axis <= 1 ) || ($diff_axis > 89 && $diff_axis < 91) || ($diff_axis > 179 && $diff_axis < 181) )
                    {
                        if($ak==17)
                        {
                            $name3 = '上升';
                            $insign = $asc['in_sign_id'];
                            $inhouse = 1;
//                        }
//                        if($ak==18)
//                        {
//                            $name3 = '天底';
//                            $insign = $ic['in_sign_id'];
//                            $inhouse = 4;
//                        }
//                        if($ak==19)
//                        {
//                            $name3 = '下降';
//                            $insign = $des['in_sign_id'];
//                            $inhouse = 7;
                        }
                        if($ak==20)
                        {
//                            $name3 = '天顶';
                            $insign = $mc['in_sign_id'];
                            $inhouse = 10;
                        }
                        $ret[] = [
                            'id1'=>$i,
//                            'name1'=>$a['name'],
                            'in_sign_id1'=>$a['in_sign_id'],
                            'in_house_id1'=>$a['in_house_id'],
                            'id2'=>$j,
//                            'name2'=>$b['name'],
                            'in_sign_id2'=>$b['in_sign_id'],
                            'in_house_id2'=>$b['in_house_id'],
                            'id3'=>$ak,
//                            'name3'=>$name3,
                            'in_sign_id3'=>$insign,
                            'in_house_id3'=>$inhouse,
                            'asp'=>$diff_axis,
//                            'output'=>$a['name_whole'].' + '.$b['name_whole'].' = '.$name3,
                        ];
                    }
                }
            }
        }
        return $ret;
    }
    /**
     * 上帝手指
     * 150°-150°-60°
     * 2-2-4
     */
    protected function god_finger($base_data = [])
    {
        $ret = [];
        for($i=0;$i<10;$i++)
        {
            $a = $base_data['planets'][$i];
            $deg1 = $a['deg'];
            for ($j = $i + 1; $j < 10; $j++)
            {
                $b = $base_data['planets'][$j];
                $deg2 = $b['deg'];
                $diff1_2 = abs($deg1 - $deg2); // 计算相差度数1-2
                if($diff1_2 > 180) $diff1_2 = 360 - $diff1_2;

                if($diff1_2 > 148 && $diff1_2 < 152)
                {
                    for ($k = 0; $k < 10; $k++)
                    {
                        $c = $base_data['planets'][$k];
                        $deg3 = $c['deg'];
                        $diff1_3 = abs($deg1 - $deg3); // 计算相差度数1-3
                        if($diff1_3 > 180) $diff1_3 = 360 - $diff1_3;
                        $diff2_3 = abs($deg2 - $deg3); // 计算相差度数2-3
                        if($diff2_3 > 180) $diff2_3 = 360 - $diff2_3;
                        if($i != $k && $j != $k
                            && (($diff1_3 > 148 && $diff1_3 < 152 && $diff2_3 > 56 && $diff2_3 < 64)
                                || ($diff1_3 > 56 && $diff1_3 < 64 && $diff2_3 > 148 && $diff2_3 < 152))
                        )
                        {
                            $ret[] = [
                                [
                                    'id'=>$i,
                                    'in_sign_id'=>$a['in_sign_id'],
                                    'in_house_id'=>$a['in_house_id'],
                                    'top'=>false,
                                ],
                                [
                                    'id'=>$j,
                                    'in_sign_id'=>$b['in_sign_id'],
                                    'in_house_id'=>$b['in_house_id'],
                                    'top'=>false,
                                ],
                                [
                                    'id'=>$k,
                                    'in_sign_id'=>$c['in_sign_id'],
                                    'in_house_id'=>$c['in_house_id'],
                                    'top'=>true,
                                ],
                            ];
//                            $ret[] = [
//                                'id1'=>$i,
////                                'name1'=>$a['name'],
//                                'in_sign_id1'=>$a['in_sign_id'],
//                                'in_house_id1'=>$a['in_house_id'],
//                                'id2'=>$j,
////                                'name2'=>$b['name'],
//                                'in_sign_id2'=>$b['in_sign_id'],
//                                'in_house_id2'=>$b['in_house_id'],
//                                'id3'=>$k,
////                                'name3'=>$c['name'],
//                                'in_sign_id3'=>$c['in_sign_id'],
//                                'in_house_id3'=>$c['in_house_id'],
//                                'top'=>$k,
////                                'output'=>'上帝手指：'
////                                    .$a['name'].'（'.mb_substr($a['in_sign_whole'],0,2,'utf-8'). ' '.$a['in_house_id'] .'宫）+'
////                                    .$b['name'].'（'.mb_substr($b['in_sign_whole'],0,2,'utf-8'). ' '.$b['in_house_id'] .'宫）+'
////                                    .$c['name'].'（'.mb_substr($c['in_sign_whole'],0,2,'utf-8').'宫）'
//                            ];
                        }

                    }
                }

            }
        }
        return $ret;
    }
    /**
     * 检查是否包含
     * @param string $string 如 0-2-6
     * @param array $arr 如 ['0-2-6-9','0-2-3-6']
     * @return bool
     */
    protected function check_in($string = '',$arr = [])
    {
        if(empty($arr)) return false;
        // 将字符串拆分成数组
        $string_arr = explode('-',$string);
        foreach ($arr as $a)
        {
            $num = 0;
            $arr_arr = explode('-',$a);
            foreach ($string_arr as $s)
            {
                if(in_array($s,$arr_arr)) $num++;
            }
            // 若包含  则立即返回true
            if(count($string_arr) == $num) return true;
        }
        return false;
    }

}
